<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :sm="24" :md="24" :xxl="16">
            <a-form-item label="区域">
              <a-row :gutter="5" type="flex">
                <a-col :span="3">
                  <a-select
                    placeholder="省"
                    v-model="queryParam.provinceCode"
                    @change="e => addressChange(e, 'cityOptions', 'province')"
                  >
                    <a-select-option v-for="item in provinceOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="3">
                  <a-select
                    placeholder="市"
                    v-model="queryParam.cityCode"
                    @change="e => addressChange(e, 'areaOptions', 'city')"
                  >
                    <a-select-option v-for="item in cityOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="3">
                  <a-select
                    placeholder="区"
                    v-model="queryParam.areaCode"
                    @change="e => addressChange(e, 'streetOptions', 'area')"
                  >
                    <a-select-option v-for="item in areaOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="3">
                  <a-select
                    placeholder="街道"
                    v-model="queryParam.streetCode"
                    @change="e => addressChange(e, '', 'street')"
                  >
                    <a-select-option v-for="item in streetOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="4">
                  <a-select
                    placeholder="商家"
                    v-model="queryParam.businessId"
                    @change="e => agentChange(e, 'networkOptions', 'agentCode')"
                  >
                    <a-select-option v-for="item in agentOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="4">
                  <a-select
                    placeholder="网点"
                    v-model="queryParam.networkId"
                    @change="e => agentChange(e, 'deviceOptions', 'networkCode')"
                  >
                    <a-select-option v-for="item in networkOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
                <a-col :span="4">
                  <a-select placeholder="设备" v-model="queryParam.deviceId">
                    <a-select-option v-for="item in deviceOptions" :key="item.name" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-col>
              </a-row>
            </a-form-item>
          </a-col>
          <a-col :sm="24" :md="12" :xl="8" v-if="!isBusiness">
            <a-form-item label="主板编号">
              <a-input placeholder="请输入" v-model="queryParam.mainboardId"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus || isBusiness">
            <a-col :sm="24" :md="12" :xl="8">
              <a-form-item label="在线状态">
                <j-dict-select-tag
                  allowClear
                  v-model="queryParam.businessStatus"
                  placeholder="请选择"
                  dictCode="businessStatus"
                />
              </a-form-item>
            </a-col>
            <a-col :sm="24" :md="12" :xl="8">
              <a-form-item label="创建时间">
                <a-range-picker allowClear class="per100" v-model="tempTimes" @change="timeChange" />
              </a-form-item>
            </a-col>
          </template>

          <a-col :sm="24" :md="12" :xl="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button @click="searchReset()" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px" v-if="!isBusiness">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- table区域 -->
    <a-table
      ref="table"
      bordered
      size="middle"
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      :scroll="{ x: 1200 }"
      @change="handleTableChange"
    >
      <template slot="deviceStatus" slot-scope="text, record">
        <a-badge status="processing" v-if="text === '0'" />
        <a-badge status="success" v-if="text === '1'" />
        <a-badge status="error" v-if="text === '2'" />
        <a-badge status="default" v-if="text === '3'" />
        {{ filterDictText(equipmentStatusOptions, text) }}
      </template>
      <template slot="businessStatus" slot-scope="text, record">
        <a-tag :color="text === '0' ? 'red' : 'green'">{{ filterDictText(businessStatusOptions, text) }}</a-tag>
      </template>
      <span slot="action" slot-scope="text, record">
        <a-button type="primary" class="ml5 mr5" size="small" @click="handleEdit(record)">拉黑用户</a-button>
      </span>
    </a-table>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { initDictOptions, filterDictText } from '@/components/dict/JDictSelectUtil'
import Api from '@/api/ServiceManage/Warnlogs'
import { RoleMixin } from '@/mixins/roleUtil'

export default {
  name: 'Warnlogs',
  mixins: [JeecgListMixin, RoleMixin],
  data() {
    return {
      filterDictText,
      url: {
        list: Api.list
      },
      columns: [],
      warnTypeOptions: [],
      equipmentStatusOptions: [],
      signalIntensityOptions: [],
      businessStatusOptions: []
    }
  },
  created() {
    this.columns = [
      {
        title: '代理商',
        align: 'center',
        dataIndex: 'agentName'
      },
      {
        title: '网点',
        align: 'center',
        dataIndex: 'networkName'
      },
      {
        title: '主板编号',
        align: 'center',
        dataIndex: 'mainboardId',
        sorter: (a, b) => {
          return a.mainboardId - b.mainboardId
        }
      },
      {
        title: '设备状态',
        dataIndex: 'deviceStatus',
        align: 'center',
        scopedSlots: { customRender: 'deviceStatus' },
        sorter: (a, b) => {
          return a.deviceStatus - b.deviceStatus
        }
      },
      {
        title: '在线状态',
        dataIndex: 'businessStatus',
        align: 'center',
        scopedSlots: { customRender: 'businessStatus' },
        sorter: (a, b) => {
          return a.businessStatus - b.businessStatus
        }
      },
      {
        title: '信号强度',
        dataIndex: 'signalIntensity',
        align: 'center',
        customRender: (text, record, index) => {
          //字典值替换通用方法
          return filterDictText(this.signalIntensityOptions, text)
        },
        sorter: (a, b) => {
          return a.signalIntensity - b.signalIntensity
        }
      },
      {
        title: '创建时间',
        align: 'center',
        dataIndex: 'createTime',
        sorter: (a, b) => {
          return a.createTime - b.createTime
        }
      }
    ]

    this.getAddress('provinceOptions')
  },
  methods: {
    // 初始化数据字典
    initDictConfig() {
      //初始化字典 - 告警类型
      initDictOptions('warnType').then(res => {
        if (res.success) {
          this.warnTypeOptions = res.result
        }
      })
      //初始化字典 - 设备状态
      initDictOptions('equipmentStatus').then(res => {
        if (res.success) {
          this.equipmentStatusOptions = res.result
        }
      })
      //初始化字典 - 信号强度
      initDictOptions('signalIntensity').then(res => {
        if (res.success) {
          this.signalIntensityOptions = res.result
        }
      })
      //初始化字典 - 在线状态
      initDictOptions('businessStatus').then(res => {
        if (res.success) {
          this.businessStatusOptions = res.result
        }
      })
    },

    // 创建时间改变
    timeChange(e, arr) {
      if (arr[0]) {
        this.queryParam.startTime = arr[0]
        this.queryParam.endTime = arr[1]
      } else {
        this.queryParam.startTime = undefined
        this.queryParam.endTime = undefined
      }
    }
  }
}
</script>

<style scoped></style>

